# !/usr/bin/python3
# -*- coding: utf-8 -*-
# ------------------------------------------
# @Time    : Date - 2021/12/15   Time - 20:43
# @Author  : Spence Guo Tang
# @FileName: _t1.py
# ------------------------------------------

from typing import List


def func(_matrix: List[List[int]]) -> int:
    if len(_matrix) == 0:
        return 0
    if len(_matrix) == 1:
        return (_matrix[0][1] - _matrix[0][0])
    area = 0
    _matrix = sorted(_matrix, key=lambda x: x[0])
    temp = _matrix[0]
    # area = temp[1] - temp[0]
    for i in range(1, len(_matrix)):
        if _matrix[i][0] >= temp[1]:
            area = area + temp[1] - temp[0]
            temp = _matrix[i]
        else:
            if _matrix[i][1] > temp[1]:
                temp = [temp[0], _matrix[i][1]]
    area = area + temp[1] - temp[0]
    return area


if __name__ == '__main__':
    n = int(input().strip())
    matrix = []
    for _ in range(n):
        matrix.append(list(map(int, input().strip().split())))
    # print(matrix)
    print(func(matrix))

